package com.ravi.controller;

import com.ravi.model.resp.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

@RestController
public class CheckTimerController {

	@GetMapping("/timer1")
	public R ScheduledExecutorService() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ScheduledExecutorService service = Executors.newScheduledThreadPool(14);
		LinkedBlockingDeque queue = new LinkedBlockingDeque<>();
		// 人被分配之后就入队
		queue.add("王丽");
		queue.add("张非");
		queue.add("马原");
		queue.add("高云");
		LinkedBlockingDeque<Object> room = new LinkedBlockingDeque<>();
		service.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				if (queue.isEmpty()) {
					return;
				}
				else {
					if (room.isEmpty()) {
						Object name = queue.pop();
						room.add(name);
						// websocket发送消息给前端
						System.out.println(name + sdf.format(new Date()) + "被叫号!");
					}else {
						return;
					}
				}
			}
		}, 0, 1, TimeUnit.SECONDS);
		service.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				if (room.isEmpty()) {
					return;
				}else {
					Object name = room.pop();
					// 计算下一诊室
					// websocket发送消息给前端
					System.out.println(name + sdf.format(new Date()) + "检查完成");
				}
			}
		}, 10, 20, TimeUnit.SECONDS);
		return R.ok();
	}
}

